29 Lecture

CS201

Midterm & Final Term Short Notes

Friend functions

In C++, a friend function is a non-member function that is given access to the private and protected members of a class. This allows the friend function to manipulate and access the private and protected data members of the class. Friend functio


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a friend function in C++? A) A function declared inside a class that can only access public members B) A member function that can access private and protected members of another class C) A non-member function that is given access to the private and protected members of a class D) A global function that is restricted from accessing the data members of a class

Answer: C

  1. Which keyword is used to declare a friend function inside a class? A) friend B) private C) public D) protected

Answer: A

  1. Can a friend function access the private members of a class? A) Yes B) No

Answer: A

  1. Which of the following is an advantage of using friend functions? A) It simplifies the implementation of certain operations B) It enhances encapsulation of class data C) It improves program maintainability D) It reduces code complexity

Answer: A

  1. Which of the following is a disadvantage of using friend functions? A) It reduces encapsulation and can make the program harder to maintain B) It requires the use of complex syntax to declare and define the function C) It can lead to security vulnerabilities in the program D) It can only be used for simple operations and cannot handle complex tasks

Answer: A

  1. Can a friend function be a member function of another class? A) Yes B) No

Answer: A

  1. What is the access level of a friend function in relation to the class it is declared in? A) Private B) Protected C) Public D) It is not a member of the class, so access level does not apply

Answer: D

  1. Can a friend function be overloaded? A) Yes B) No

Answer: A

  1. Can a friend function be declared in the private section of a class? A) Yes B) No

Answer: A

  1. Which of the following is an example of a scenario where a friend function would be useful? A) To add two numbers together B) To sort a list of objects C) To calculate the distance between two objects in a class D) To print the contents of a private data member

Answer: D



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a friend function in C++? A friend function in C++ is a function that is not a member of a class but has access to the private and protected members of the class.

  2. What is the syntax for declaring a friend function? The syntax for declaring a friend function is as follows: friend return_type function_name(class_name &object);

  3. What is the difference between a friend function and a member function? A friend function is not a member of the class, but has access to the private and protected members of the class. A member function is a function that is a part of the class and has access to all the members of the class.

  4. Can a friend function be called without an object of the class? Yes, a friend function can be called without an object of the class.

  5. Can a friend function be declared inside a class? Yes, a friend function can be declared inside a class.

  6. Can a friend function be inherited by the derived class? No, a friend function cannot be inherited by the derived class.

  7. What is the difference between a friend class and a friend function? A friend class is a class that has access to the private and protected members of another class. A friend function is a function that has access to the private and protected members of a class.

  8. What is the use of a friend function? A friend function is used to access the private and protected members of a class from outside the class.

  9. Can a friend function be overloaded? Yes, a friend function can be overloaded.

  10. What is the scope of a friend function? The scope of a friend function is global, but it has access to the private and protected members of the class.

In C++, a friend function is a function that is not a member of a class but has access to the private and protected members of the class. This means that a friend function can access and modify the private and protected data members of a class, without needing to create an object of that class. A friend function can be declared in the public, protected or private section of a class. The declaration of a friend function is preceded by the keyword 'friend', followed by the return type, function name and arguments. One common use of a friend function is to overload operators. For example, the insertion operator '<<' can be overloaded as a friend function to allow objects of a class to be printed to the console or written to a file. Another use of a friend function is to provide a function that is related to the class, but does not belong to the class. For example, a function that calculates the distance between two objects of a class can be declared as a friend function. It is important to note that a friend function does not have access to the 'this' pointer of the class, and cannot call other member functions of the class. Additionally, a friend function is not a member of the class, and therefore cannot be called using the dot operator. In summary, friend functions in C++ provide a mechanism to access the private and protected members of a class from outside the class, without requiring an object of the class to be created.